1 /**
2 The following examples come from
3 $(LINK http://zetcode.com/databases/mysqltutorial/constraints/).
4  */
5 module test.examples_enum_constraint;
6 
7 version(D_Ddoc)
8 {
9     ///
10     class BlankClassSoDocsWillBeGenerated { }
11 }
12 
13 
14 /**
15 This example is for the ENUM constraints. The table
16 in SQL can be created by
17 $(D $(D $(D sql
18 CREATE TABLE Shops
19 (
20     Id INTEGER NOT NULL PRIMARY KEY,
21     Name VARCHAR(55),
22     Quality ENUM('High', 'Average', 'Low')
23 );
24 
25 )))
26  */
27 unittest
28 {
29     import db_constraints;
30 
31     class Shop
32     {
33         private int _Id;
34         @PrimaryKeyColumn @NotNull
35         @property int Id()
36         {
37             return _Id;
38         }
39         @property void Id(int value)
40         {
41             setter(_Id, value);
42         }
43         private string _Name;
44         @property string Name()
45         {
46             return _Name;
47         }
48         @property void Name(string value)
49         {
50             setter(_Name, value);
51         }
52         private string _Quality;
53         // Quality can only have a value that
54         // is among the enumeration below.
55         // Using false so this will not throw an
56         // exception but instead just change _Quality
57         // to an empty string.
58         @EnumConstraint!(false, "High", "Average", "Low")
59         @property string Quality()
60         {
61             return _Quality;
62         }
63         @property void Quality(string value)
64         {
65             setter(_Quality, value);
66         }
67         this(int Id_, string Name_, string Quality_)
68         {
69             this._Id = Id_;
70             this._Name = Name_;
71             this._Quality = Quality_;
72             initializeKeyedItem();
73         }
74 
75         mixin KeyedItem!();
76     }
77 
78     auto Boneys = new Shop(1, "Boneys", "High");
79     assert(Boneys.Quality == "High");
80 
81     auto ACRiver = new Shop(2, "AC River", "Average");
82     assert(ACRiver.Quality == "Average");
83 
84     auto AT34 = new Shop(3, "AT 34", "**");
85     // since we have this as a non-strict enum, the
86     // quality is set to an empty string when
87     // given a string that is not in the enumeration
88     assert(AT34.Quality == "");
89 }